/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* FrameUtil.java
*
* Created on November 24, 2007, 5:15 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
 */
package javautil.swing;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author stewari1
 */
public class FrameUtil {

    /** Creates a new instance of FrameUtil */
    public FrameUtil() {}

    public static void centralize(Component comp, int inset) {

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        comp.setBounds(inset, inset, screenSize.width - inset * 2, screenSize.height - inset * 2);
    }

    public static void addWindowClosingWarning(final JFrame frame) {

        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent evt) {

                int val = 0;

                try {

                    val = JOptionPane.showConfirmDialog(frame,
                            "Do you want to close the application ?", "Exit ?",
                            JOptionPane.YES_NO_OPTION);

                } catch (Exception ex) {
                    ex.printStackTrace();
                }

                if (val == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }
            }

        });
    }
}
